package leducHoldem.Game;

import java.util.Scanner;

import leducHoldem.Data.Board;
import leducHoldem.Data.Dealer;
import leducHoldem.Data.Move;
import leducHoldem.Players.CFRPlayer;
import leducHoldem.Players.HumanPlayer;
import leducHoldem.Players.MCTSPlayer;
import leducHoldem.Players.OverconfidentPlayer;
import leducHoldem.Players.ParanoidPlayer;
import leducHoldem.Players.Player;

public class GameRunner {
	
	private static Board board;
	
	private static Dealer dealer;
	
	private static Player firstPlayer = null;
	private static Player secondPlayer = null;
	private static Player currentPlayer = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		String playerOne;
		String playerTwo;
		

		System.out.println("******************************************");
		System.out.println("Welcome in game Leduc holdem poker");
		System.out.println("******************************************");
		
		
		dealer = new Dealer();

		dealer.shuffle();

		Move firstCard = dealer.dealCard();
		Move secondCard = dealer.dealCard();

		do {
			System.out.println("Please choose player one from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerOne = scanner.next();

			if (playerOne.equals("1")) {
				firstPlayer = new MCTSPlayer(firstCard, (byte)1);
			} else if (playerOne.equals("2")) {
				firstPlayer = new CFRPlayer(firstCard, (byte)1);
			} else if (playerOne.equals("3")) {
				firstPlayer = new ParanoidPlayer(firstCard, (byte)1);
			} else if (playerOne.equals("4")) {
				firstPlayer = new OverconfidentPlayer(firstCard, (byte)1);
			} else if (playerOne.equals("5")) {
				firstPlayer = new HumanPlayer(firstCard, (byte)1);
			}
		} while (firstPlayer == null);
		
		do {
			System.out.println("Please choose player two from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerTwo = scanner.next();

			if (playerTwo.equals("1")) {
				secondPlayer = new MCTSPlayer(secondCard, (byte)2);
			} else if (playerTwo.equals("2")) {
				secondPlayer = new CFRPlayer(secondCard, (byte)2);
			} else if (playerTwo.equals("3")) {
				secondPlayer = new ParanoidPlayer(secondCard, (byte)2);
			} else if (playerTwo.equals("4")) {
				secondPlayer = new OverconfidentPlayer(secondCard, (byte)2);
			} else if (playerTwo.equals("5")) {
				secondPlayer = new HumanPlayer(secondCard, (byte)2);
			}
		} while (secondPlayer == null);
		
		System.out.println("First player has " + firstCard);
		System.out.println("Second player has " + secondCard);
		
		board = new Board(firstCard, secondCard);
		currentPlayer = firstPlayer;
		
		new GameRunner().runGame();

	}
	
	private void runGame() {

		Move move;
		int value = 0;

		while (board.isTerminal() == 0) {

			System.out.println();
			System.out.println("Table: " + board.table);
			System.out.println("Pot: " + board.getPot());
			System.out.println("Opponent's action: " + board.getLastMove());
			System.out.println("Current player: " + board.currentPlayer);
			System.out.println("Id: " + board.id);
			System.out.println("Available actions: " + board.availableActions);
			System.out.println();

			do {
				move = currentPlayer.getNextNode(new Board(board), true);
			} while (!board.isValid(move));

			value = board.makeMove(move);

			if (value == 2) {
				board.makeMove(dealer.dealCard());
			}

			switchPlayers(value);
		}

		System.out.println();
		System.out.println("Board id: " + board.id);
		System.out.println("Table: " + board.table);
		System.out.println("Pot: " + board.getPot());
		System.out.println("Opponent's action: " + board.getLastMove());
		System.out.println();

		System.out.println("Player " + board.isTerminal() + " won.");

	}

	private void switchPlayers(int value) {

		if (value == 2) {
			currentPlayer = firstPlayer;
			return;
		}

		if (currentPlayer.sign == 1) {
			currentPlayer = secondPlayer;
			return;
		}

		currentPlayer = firstPlayer;

	}
}
